package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.CheckPayPasswordCashCreditBean;
import com.sikao.app.bean.CheckPayPasswordCashCreditSubmitBean;
import com.sikao.app.dao.CheckPayPasswordCashCreditModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.Utils;


/**
 * 发布商品第一步，当挂单方式为保证金挂单时候 付款
 */
public class PayForReleaseGoodsActivity extends BaseActivity implements View.OnClickListener {


	private RelativeLayout rl;

	private int publishType=0;//挂单方式 1仓单 2保证金 3信誉

   private  String geld="0.00";
   private  String balance="0.00";

   private  String credit="0.000";//消耗的信誉
   private  String ownCredit="0.000";//我的剩余信誉




	private LinearLayout llForCash;
	private TextView tvGeld;//支付款
	private TextView tvBalance;//余额
	private LinearLayout llForCredit;
	private TextView tvUseCredit;
	private TextView tvMyCredit;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_pay_for_release_goods);
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void initData() {
		publishType=getIntent().getIntExtra("publishType",0);//挂单方式 1仓单 2保证金 3信誉
		if(publishType==2) {//挂单方式 1仓单 2保证金 3信誉
			geld = getIntent().getStringExtra("geld");
			balance = getIntent().getStringExtra("balance");
		}else if (publishType==3) {
			credit = getIntent().getStringExtra("credit");
			ownCredit = getIntent().getStringExtra("ownCredit");
		}
	}

	@Override
	protected void initView() {
		rl = (RelativeLayout) findViewById(R.id.rl);
		llForCash = (LinearLayout) findViewById(R.id.ll_for_cash);
		tvGeld = (TextView) findViewById(R.id.tv_geld);
		tvBalance = (TextView) findViewById(R.id.tv_balance);
		llForCredit = (LinearLayout) findViewById(R.id.ll_for_credit);
		tvUseCredit = (TextView) findViewById(R.id.tv_use_credit);
		tvMyCredit = (TextView) findViewById(R.id.tv_my_credit);

		if(publishType==2){//挂单方式 1仓单 2保证金 3信誉
			llForCash.setVisibility(View.VISIBLE);
			llForCredit.setVisibility(View.GONE);
		}else if (publishType==3){
			llForCash.setVisibility(View.GONE);
			llForCredit.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void initListener() {
		findViewById(R.id.bt_cancle).setOnClickListener(this);
		findViewById(R.id.bt_confirm).setOnClickListener(this);
	}

	@Override
	protected void init() {
		tvGeld.setText(geld);
		tvBalance.setText(balance);

		tvUseCredit.setText(credit);
		tvMyCredit.setText(ownCredit);
	}

	private EditText getEtPassword(){
		return (EditText) findViewById(R.id.et_password);
	}
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.bt_cancle:
				//TODO implement
				//Intent intent = new Intent();
			//	setResult(ReleaseGoodsActivity.STEP_PRICE_RESULT_CODE, intent);
				finish();
				break;
			case R.id.bt_confirm:

				checkPayPasswordCashCredit();

				break;
		}
	}

	private void checkPayPasswordCashCredit() {
		CheckPayPasswordCashCreditSubmitBean checkPayPasswordCashCreditSubmitBean=new CheckPayPasswordCashCreditSubmitBean();


		if(publishType==2){//挂单方式 1仓单 2保证金 3信誉
			checkPayPasswordCashCreditSubmitBean.setCash((Utils.roundForNumber(geld,2)));
			checkPayPasswordCashCreditSubmitBean.setCredit("0.000");
		}else if (publishType==3){
			checkPayPasswordCashCreditSubmitBean.setCash("0.00");
			checkPayPasswordCashCreditSubmitBean.setCredit((Utils.roundForNumber(credit,3)));
		}
		checkPayPasswordCashCreditSubmitBean.setPayPassword(TextUtils.isEmpty(getEtPassword().getText())?"0":getEtPassword().getText().toString());

		new CheckPayPasswordCashCreditModel(this, new OnNetBackListener<CheckPayPasswordCashCreditBean>() {
			@Override
			public void netBack(CheckPayPasswordCashCreditBean data) {
				if (data == null) {
					Toast.makeText(PayForReleaseGoodsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						Intent intent = new Intent();
						intent.putExtra("payPassword",TextUtils.isEmpty(getEtPassword().getText())?"0":getEtPassword().getText().toString());
						setResult(ReleaseGoodsActivity.PAY_RESULT_CODE, intent);
						finish();
					} else {
						Toast.makeText(PayForReleaseGoodsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}

			}
		}).request(JSONUtil.toJSON(checkPayPasswordCashCreditSubmitBean),true);

	}


}
